
(function(){
  'use strict';
  var qs = new URLSearchParams(location.search);
  var isEP = (qs.get('view') === 'ep') || /\/playlist-ep\.html$/i.test(location.pathname);
  if(!isEP) return;
  document.addEventListener('DOMContentLoaded', init);
  function $(s,c){ return (c||document).querySelector(s); }
  function $all(s,c){ return Array.from((c||document).querySelectorAll(s)); }
  function getSort(){
    var s = (qs.get('sort')||'latest').toLowerCase();
    if(s==='new') s='latest';
    return s;
  }
  function init(){
    document.body.classList.add('view-ep');
    // ensureSwitch(); // simplified per request
    // ensureThemeToggle(); // simplified per request
    decorateHero();
    observeRows();
    installLoadMore();
  }
  function decorateHero(){
    var ht = document.getElementById('hero-title');
    var hd = document.getElementById('hero-desc');
    var he = document.getElementById('hero-emoji');
    if(ht) ht.textContent = '最新上架 · 精选歌单';
    /* desc removed by design */
    /* emoji removed by design */
  }
  
function ensureSwitch(){
    if($('.ep-switch')) return;
    var sort = getSort();
    var bar = document.createElement('div'); bar.className='ep-switch';
    // 优先使用 playlist-ep.html，避免某些环境忽略查询参数
    var toLatest = 'playlist-ep.html?sort=new';
    var toPopular = 'playlist-ep.html?sort=popular';
    var aLatest = document.createElement('a'); aLatest.href = toLatest; aLatest.textContent = '新上架';
    var aPopular = document.createElement('a'); aPopular.href = toPopular; aPopular.textContent = '播放最多';
    bar.appendChild(aLatest); bar.appendChild(aPopular);
    var anchor = $('.hero .container') || $('#hero') || document.body; anchor.appendChild(bar);
    $all('.ep-switch a').forEach(function(a){
      var s = new URL(a.href, location.href);
      var v = (new URLSearchParams(s.search).get('sort')||'').toLowerCase();
      if(v==='new') v='latest';
      if(v === sort) a.classList.add('active');
    });
  }
);
  }
  function observeRows(){
    var table = document.querySelector('.tracks table.table'); if(!table) return;
    var enhance = function(){
      var i=0; var rows = table.querySelectorAll('tbody tr');
      rows.forEach(function(tr){
        if(tr.classList.contains('ep-row')) return; tr.classList.add('ep-row');
        var firstTd = tr.querySelector('td');
        if(firstTd && !firstTd.querySelector('.ep-thumb')){
          var thumb = document.createElement('span'); thumb.className='ep-thumb';
          var play = document.createElement('span'); play.className='ep-play';
          play.innerHTML = '<svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path fill="currentColor" d="M8 5v14l11-7z"/></svg>';
          thumb.appendChild(play); firstTd.insertBefore(thumb, firstTd.firstChild);
        }
        var tds = tr.querySelectorAll('td');
        for(var k=0;k<tds.length;k++){ if(/title|曲名|名称/i.test(tds[k].textContent)){ tds[k].classList.add('ep-title'); break; } }
        var badgeNeeded=false,badgeClass='',badgeText='';
        if(getSort()==='latest'){ badgeNeeded = (i<20); badgeClass='new'; badgeText='NEW'; }
        else { badgeNeeded = (i<20); badgeClass='hot'; badgeText='HOT'; }
        if(badgeNeeded){
          var titleCell = tr.querySelector('.ep-title') || tr.children[1] || tr.children[0];
          if(titleCell && !titleCell.querySelector('.badge')){
            var b=document.createElement('span'); b.className='badge '+badgeClass; b.textContent=badgeText;
            titleCell.insertBefore(b, titleCell.firstChild);
            titleCell.style.display='flex'; titleCell.style.alignItems='center'; titleCell.style.gap='8px';
          }
        }
        i++;
      });
    };
    enhance();
    var mo = new MutationObserver(function(m){ if(m.some(function(rec){ return rec.addedNodes && rec.addedNodes.length; })) enhance(); });
    mo.observe(table, {subtree:true, childList:true});
  }
  function installLoadMore(){
    var table = document.querySelector('.tracks table.table'); if(!table) return;
    var tbody = table.querySelector('tbody'); if(!tbody) return;
    var reveal = 40;
    function applyLimit(){
      var rows = Array.from(tbody.querySelectorAll('tr'));
      rows.forEach(function(r,i){ r.style.display = (i<reveal)?'':'none'; });
      btn.hidden = (rows.length <= reveal);
    }
    var wrap = document.createElement('div'); wrap.className='ep-loadmore-wrap';
    var btn = document.createElement('button'); btn.className='btn ghost ep-loadmore'; btn.textContent='加载更多';
    btn.addEventListener('click', function(){ reveal += 40; applyLimit(); });
    wrap.appendChild(btn);
    table.parentNode.insertBefore(wrap, table.nextSibling);
    var mo = new MutationObserver(applyLimit); mo.observe(tbody, {childList:true}); applyLimit();
  }
})();    


  function ensureThemeToggle(){
    // Create a small theme switcher inside the EP switch bar
    var bar = document.querySelector('.ep-switch');
    if(!bar) return;
    if(bar.querySelector('.ep-theme-toggle')) return;
    var btn = document.createElement('button');
    btn.type = 'button';
    btn.className = 'ep-theme-toggle';
    btn.setAttribute('aria-label','切换主题');
    function icon(theme){
      return theme==='dark' ? '🌙' : '🌞';
    }
    function current(){ return document.documentElement.getAttribute('data-theme') || 'light'; }
    btn.textContent = icon(current());
    btn.addEventListener('click', function(){
      var t = current()==='dark' ? 'light' : 'dark';
      document.documentElement.setAttribute('data-theme', t);
      try{ localStorage.setItem('theme', t); }catch(e){}
      btn.textContent = icon(t);
      // If there is a global toggle button, keep it in sync visually
      var toggle = document.getElementById('rt-theme-toggle');
      if(toggle && toggle.setAttribute){
        toggle.setAttribute('data-state', t);
      }
    });
    // Add a small divider
    var sep = document.createElement('span');
    sep.className = 'ep-sep';
    sep.setAttribute('aria-hidden','true');
    bar.appendChild(sep);
    bar.appendChild(btn);
  }
